// pages/login/login.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show:false,
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    userName: "", // 用户名
    userPwd: "", // 密码
    inputValue: "" ,
    usweiid:"",
    butShow:false // 授权按钮显示隐藏

  },
  onLoad: function (options) {
    this.userInfoHandler();

    this.setData({
      usweiid: options.usweiid
    })
  }, userNames: function (e) {
    this.setData({
      userName: e.detail.value
    });
    console.info("用户名");
  }, userPwds: function (e) {
    this.setData({
      userPwd: e.detail.value
    });
    console.info("密码");
  }, primary: function (e) {
    const that = this;
    console.info(this.data.userName + "---" + this.data.userPwd)

    if (this.data.userName == "" || this.data.userName == null || this.data.userPwd == "" || this.data.userPwd == null){
      wx.showModal({
        title: '提示',
        content: '用户名密码不能为空',
        success: function (res) {
          // if (res.confirm) {
          //   console.log('用户点击确定')
          // } else {
          //   console.log('用户点击取消')
          // }

        }
      })
      return;
    }

    var url = app.globalData.url;

    wx.request({
      url: url+'drugManager/findManager',
      data:{
        manUser: this.data.userName,
        manPwd: this.data.userPwd,
        usweiid: that.data.usweiid
      },header:{
        'content-type': 'application/json' // 默认值
      },success(res){
        if (res.data.num == 0) {

          if (res && res.header && res.header['Set-Cookie']) {
            console.info(res.header['Set-Cookie']+"-====--============")
            wx.setStorageSync('cookieKey', res.header['Set-Cookie']);//保存Cookie到Storage
           
          }
          console.info("登录成功")
          wx.switchTab({
              url: '../home/home',
            })
        }else{
          wx.showModal({
            title: '提示',
            content: '账号密码错误，请重新输入',
            success: function (res) {
              
            }
          })
        }
      }
    })
  },
  bindKeyInput: function (e) {
    this.setData({
      inputValue: e.detail.value
    });
    console.info(inputValue);
  }, userInfoHandler: function (e) {
    console.log(e)
    let that = this;
    // console.log(e)
    // 获取用户信息
    wx.getSetting({
      success(res) {
        // console.log("res", res)
        if (res.authSetting['scope.userInfo']) {
          that.setData({
            butShow:false
          })
          console.log("已授权=====")
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称
          wx.getUserInfo({
            success(res) {
              console.log("获取用户信息成功", res)
              that.setData({
                name: res.userInfo.nickName
              })
            },
            fail(res) {
              console.log("获取用户信息失败", res)
            }
          })
        } else {
          that.setData({
            butShow: true
          })
          console.log("未授权=====")
          //that.showSettingToast("请授权")
        }
      }
    })
  }
})